package com.taplinker.client.im.activity.message;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;
import com.taplinker.client.R;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.TimeUtil;
import com.ti.winona.util.RegexUtil;

public class LeftTextMessage extends AbstractMessageView{
//	private TapLinkerConversation conversation;

	@Override
	public View getMessageView(final Context context, final TapLinkerMessage message, View convertView) {
		TextMessageHolder textMessageHolder = null;
		if(convertView == null) {
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			textMessageHolder = new TextMessageHolder();
			textMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			textMessageHolder.head = (SmartImageView) convertView.findViewById(R.id.head_icon_iv);
			textMessageHolder.content = (TextView) convertView.findViewById(R.id.content_text_tv);
			textMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			convertView.setTag(textMessageHolder);
		} else {
			textMessageHolder = (TextMessageHolder) convertView.getTag();
		}
		String name = message.getSendUser();
//		if (!TextUtils.isEmpty(conversation.getCustomerId())) {
//			// name = name.substring(name.lastIndexOf('_'));
//			name =  message.getSendUser();
//		}
		if (name != null && name.length() > 0) {
			name = message.getSendSdkAccount();
			name = name.substring(name.lastIndexOf('_') + 1);
		}
		textMessageHolder.name.setText(name);
//		if(!TextUtils.isEmpty(message.getIcon())) {
//			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
//			ImageManager.getInstance().loadImage(headUrl, textMessageHolder.head, ImageUtil.dip2px(context, 45), ImageUtil.dip2px(context, 45),null);
			// 取出对方的头像地址
//			SharedPreferences sp = context.getSharedPreferences(Base1Activity.SP_FILE_NAME, Context.MODE_APPEND);
//			String headUrl = sp.getString(IMQueueActivity.OTHER_SIDE_IMAGE_URL, null);
//			if( headUrl!=null && !"".equals(headUrl) ) {
//				textMessageHolder.head.setImageUrl(headUrl);
//			}
//			Log.v("log_ktian", "她的头像TEXT:"+headUrl);
//		}
		SmileyParser parser = new SmileyParser(context);
		String content = message.getContent();
		if(!TextUtils.isEmpty(content)) {
			if(content.contains("&nbsp") || content.contains("nbsp")) {
				content = content.replaceAll("?nbsp", "");
			}
		}
		
		textMessageHolder.content.setText(parser.replace(content));
		textMessageHolder.content.setOnLongClickListener(new MessageOnLongClickListener(message, context));
		textMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		textMessageHolder.content.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = "";
				// 过滤出http连接
				url = RegexUtil.parseUrl(message.getContent());
				// 点击打开网页地址
				if(url!=null && !"".equals(url)) {
					Intent intent = new Intent();        
			        intent.setAction("android.intent.action.VIEW");
			        Uri content_url = Uri.parse(url);   
			        intent.setData(content_url);  
			        context.startActivity(intent);
				}
			}
		});
		return convertView;
	}
	
	class TextMessageHolder {
		TextView name;		// 用户名
		SmartImageView head;		// 头像
		TextView content;	// 内容
		TextView time;		// 时间
	}

}
